package com.itheima.travel.web.base;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * 约定: 请求标识就是要调用的方法
 * 基础的Servlet,用于获取请求标识,反射调用标识对应的方法
 */
public class BaseServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("this代表的对象为: "+this);
        try {
            //1.获取请求标识
            String action = request.getParameter("action");
            //2.获取调用者类的字节码对象
            Class clazz = this.getClass();
            //3.获取要执行的方法的字节码对象
            Method method = clazz.getMethod(action, HttpServletRequest.class, HttpServletResponse.class);
            //4.反射执行方法
            method.invoke(this,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}